package com.hmall.gateway.filters;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

//和GlobalFilter不一样的是，定义完不会立即生效，需要在yaml文件中做配置
//也可以有动态配置参数，不过实现起来比较复杂--不写了
@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    //使用装饰类OrderedGatewayFilter 定义GatewayFilter 和 实现优先级---由于传统的匿名内部类无法实现某个接口，需要显式的实现类
    @Override
    public GatewayFilter apply(Object config) {
        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println("print any filter running");
                return chain.filter(exchange);
            }
        }, 1
        );
    }


//    @Data
//    public static class config {
//        private String a;
//        private String b;
//        private String c;
//    }
//
//    public PrintAnyGatewayFilterFactory() {
//        super(config.class);
//    }
//
//    @Override
//    public List<String> shortcutFieldOrder() {
//        return List.of("a","b","c");
//    }
}